#include "argwindow.h"
#include "ui_argwindow.h"

#include "mainwindow.h"


#include "Aria.h"
#include "ArNetworking.h"





ArgWindow::ArgWindow(QWidget *parent,ArArgumentParser *argParser) :
    QMainWindow(parent),
    ui(new Ui::ArgWindow)
{
    ui->setupUi(this);
    setFixedSize(this->geometry().width(),this->geometry().height());
    setWindowFlags(Qt::SplashScreen);



    ui->text_direction->setText(argParser->checkParameterArgument("host",true));
    //ui->text_user->setText(argParser->checkParameterArgument("user",true));
    //ui->text_password->setText(argParser->checkParameterArgument("password",true));






      parser = argParser;


}

ArgWindow::~ArgWindow()
{
    client.disconnect();
    Aria::shutdown();

    delete ui;
}



void ArgWindow::on_button_exit_clicked()
{
    exit(EXIT_SUCCESS);
}





void ArgWindow::on_button_connect_clicked()
{
    ui->label_log->setText("Connecting......");
    Aria::init();

    char **argumentos = parser->getArgv();
    for (int i=0; i<parser->getArgc(); i++)
        printf("Arg %d: %s\n",i,argumentos[i]);


    char dirstr[128];
    sprintf(dirstr,"-host %s",ui->text_direction->text().toUtf8().constData());

    printf("Direccion :  %s",dirstr);

    parser->addDefaultArgument(dirstr);


    ArClientSimpleConnector clientConnector(parser);





    if (!Aria::parseArgs() || !parser->checkHelpAndWarnUnparsed())
      {
        ui->label_log->setText("Parse error, restart....");
      }

      /* Connect our client object to the remote server: */
      if (!clientConnector.connectClient(&client))
      {
          char men [50];
        if (client.wasRejected())
          sprintf(men,"Server '%s' rejected connection, exiting", client.getHost());
        else
          sprintf(men,"Could not connect to server '%s', exiting", client.getHost());
        ui->label_log->setText(men);
      } else {

      MainWindow *w = new MainWindow(0,&client);
      w->show();
      this->hide();

      }
}
